﻿using System;
using System.Windows.Forms;
using NanoERP.Infrastructure;
using NanoERP.Business.BaseInfo;
using NanoERP.Model;
using NanoERP.Common;

namespace NanoERP.BaseInfo
{
    [Module("FrmLogisticsList")]
    public partial class FrmLogisticsList : ModuleForm
    {
        readonly LogisticsinfoBLL _logisticsBll;
        public FrmLogisticsList()
        {
            InitializeComponent();

            _logisticsBll = new LogisticsinfoBLL();

            QueryInfo();
        }

        /// <summary>
        /// 
        /// </summary>
        void QueryInfo()
        {
            dgvList.AutoGenerateColumns = false;
            dgvList.DataSource = _logisticsBll.Query(txtKey.Text.Trim());
        }

        private void tsbAdd_Click(object sender, EventArgs e)
        {
            var f = new FrmLogisticsEdit();
            if (f.ShowDialog() == DialogResult.OK)
            {
                QueryInfo();
            }
        }

        private void tsbEdit_Click(object sender, EventArgs e)
        {
            if (dgvList.CurrentRow == null) return;
            var id = ((Clientlogisticsinfo)dgvList.CurrentRow.DataBoundItem).Id;

            var obj = _logisticsBll.Get(id);
            var f = new FrmLogisticsEdit(obj);
            if (f.ShowDialog() == DialogResult.OK)
            {
                QueryInfo();
            }
        }

        private void tsbDelete_Click(object sender, EventArgs e)
        {
            if (dgvList.CurrentRow == null) return;
            var obj = dgvList.CurrentRow.DataBoundItem as Clientlogisticsinfo;
            var text = string.Format(@"您要确定要“删除”当前物流单位“{0}”吗？", obj.Clientname);
            if (MessageBox.Show(text, "请确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                _logisticsBll.Delete(obj.Id);
                QueryInfo();
            }
        }

        private void tsbPrint_Click(object sender, EventArgs e)
        {

        }

        private void tsbExport_Click(object sender, EventArgs e)
        {
            string msgLog = string.Empty;
            ExcelUtil.ExportToExcel(this.dgvList, "物流公司信息", out msgLog);
            if (msgLog != null)
            {
                MessageBox.Show(msgLog, "消息", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void tsbExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            QueryInfo();
        }

        private void dgvList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex == -1)
                return;
            tsbEdit_Click(null, null);
        }

        private void dgvList_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            ControlUtil.DataGridRowPostPaint(sender, e);
        }
    }
}
